import { message } from 'ant-design-vue/lib'
export default function ({ $axios, redirect, $cookies }) {
  const cookName = $cookies.get('name')
  console.log($cookies.get('name'), cookName, 'aaaa$cookies')
  $axios.create({
    // baseURL: process.env.BASE_API, // config/dev.env 的 base_url
    timeout: 5000, // 请求超时时间
    headers: {
      'content-Type': ''
    }
  })

  $axios.interceptors.request.use(
    (config) => {
      /* config.headers = {
        'tokens': cookName ||  '',
        ...config.headers
      } */
      return config
    },
    (error) => {
      console.log(error, 'eddd')
      return Promise.reject(error)
    }
  )
  $axios.onRequest((config) => {
    return config
  }, (error) => {
    return Promise.reject(error)
  })

  $axios.interceptors.response.use(
    (response) => {
      const res = response.data
      if (response.status === 200 || res.code === 0) {
        return res
      } else {
        redirect('/404')
      }
      /* if (response.status !== 404) {
        return res
      } else {
        redirect('/404')
      } */
      return Promise.reject(new Error(res.msg || 'Error'))
    },
    (error) => {
      return Promise.reject(error)
    }
  )

  $axios.onError((error) => {
    const code = parseInt(error.response && error.response.status)
    if (code === 400) {
      redirect('/404')
    }
    if (code === 504) {
      message.error('服务器错误', 500)
    }
    return error // 一定要返回
  })
}
